/*
 * Copyright 2011-2013 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


Components.utils.import("resource://oaa-accessibility-extension/cache-inspector-common.js");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
// Components.utils.import("resource:///modules/devtools/gcli.jsm");


/**
 * @file sidebar-rule-summary.js
 *
 * @desc Supports the sidebar showing rule summary information for a list if cache items 
 */

/* ---------------------------------------------------------------- */
/*            OAA_CI and OAA_AE_SIDEBAR.ElementView Object             */ 
/* ---------------------------------------------------------------- */

if (typeof OAA_CI === "undefined") {
  var OAA_CI = {};
}

if (typeof OAA_AE_SIDEBAR.ElementView === "undefined") {
  OAA_AE_SIDEBAR.ElementView = {};
}


/* ---------------------------------------------------------------- */
/*       Rule Category Cache Item Views and View Controllers        */ 
/* ---------------------------------------------------------------- */

/**
 * @function OAA_AE_SIDEBAR.ElementView.init
 *
 * @desc Initializes the view
 */
 
OAA_AE_SIDEBAR.ElementView.init = function () {

};

/**
 * @function OAA_AE_SIDEBAR.ElementView.initStrings
 *
 * @desc Initializes message strings for view
 */
 
OAA_AE_SIDEBAR.ElementView.initStrings = function () {

  if (typeof this.MSG_SELECT_RULE !== 'string') {

    var nls_strings  = OAA_AE_SIDEBAR.nls_strings;

    if (!nls_strings) return;

    this.MSG_UPDATING                 = nls_strings.getString('updating');
    this.MSG_SELECT_ELEMENT           = nls_strings.getString('selectElement');
    this.MSG_FILTERED_ITEM            = nls_strings.getString("filteredItem");
    this.MSG_FILTERED_ITEMS           = nls_strings.getString("filteredItems");
    this.MSG_NO_ELEMENT_SELECTED      = nls_strings.getString("noElementSelected");
    this.MSG_INFORMATION              = nls_strings.getString("information");
    this.MSG_ELEMENT                  = nls_strings.getString("element");
    this.MSG_MORE_INFORMATION_RULE    = nls_strings.getString('moreInformationRule');
    this.MSG_NO_ELEMENTS              = nls_strings.getString('noElements');
    this.MSG_RULE_RESULTS_FOR         = nls_strings.getString('ruleResultsFor');

  }
  
};


/**
 * @function OAA_AE_SIDEBAR.ElementView.hide
 *
 * @desc Hides the rule category sidebar
 */
 
OAA_AE_SIDEBAR.ElementView.hide = function () {

  var vbox_element_view  = window.document.getElementById('ID_VBOX_ELEMENT_VIEW');
  vbox_element_view.hidden = true;
  
};


/**
 * @function OAA_AE_SIDEBAR.ElementView.show
 *
 * @desc Shows the rule category sidebar and updates the contents
 *
 * @param {Boolean}  tree  - True if the elements has a tree representation, otherwise false
 */
 
OAA_AE_SIDEBAR.ElementView.show = function (tree) {

  var vbox_element_view  = window.document.getElementById('ID_VBOX_ELEMENT_VIEW');
  vbox_element_view.hidden = false;
  
  var treecol_primary  = window.document.getElementById('ID_TREECOL_ELEMENT_VIEW_ELEMENT');

  if (typeof tree === 'boolean' && tree) treecol_primary.setAttribute('primary', 'true');
  else treecol_primary.setAttribute('primary', 'false');
    
};

/**
 * @function OAA_AE_SIDEBAR.ElementView.showTreeControls
 *
 * @desc Shows the rule category sidebar and updates the contents
 *
 * @param {Boolean}  flag  - Boolean indicating whether tree controls should be hidden
 */
 
OAA_AE_SIDEBAR.ElementView.showTreeControls = function (flag) {

  if (typeof flag !== 'boolean') flag = true;

  var tree_controls = document.getElementById('ID_HBOX_ELEMENT_VIEW_EXPAND');
  
  tree_controls.setAttribute('hidden', 'false');

  var order_treecol = document.getElementById('ID_TREECOL_ELEMENT_VIEW_ORDER');
  var primary_treecol = document.getElementById('ID_TREECOL_ELEMENT_VIEW_ELEMENT');
  
  var show_tree = document.getElementById('ID_BUTTON_SHOW_TREE');
  var expand_tree = document.getElementById('ID_BUTTON_EXPAND_TREE');
  var collapse_tree = document.getElementById('ID_BUTTON_COLLAPSE_TREE');
  
  if (flag) {
    order_treecol.setAttribute('hidden', 'true');
    primary_treecol.setAttribute('primary', 'true');
    
    show_tree.setAttribute('disabled', 'true');
    expand_tree.setAttribute('disabled', 'false');
    collapse_tree.setAttribute('disabled', 'false');    
  } 
  else {
    order_treecol.setAttribute('hidden', 'false');
    primary_treecol.setAttribute('primary', 'false');
    
    show_tree.setAttribute('disabled', 'false');
    expand_tree.setAttribute('disabled', 'true');
    collapse_tree.setAttribute('disabled', 'true');
  } 
  
};

/**
 * @function OAA_AE_SIDEBAR.ElementView.showTreeControls
 *
 * @desc Shows the rule category sidebar and updates the contents
 *
 * @param {Boolean}  flag - true if all expanded, otherwise false if all collapsed
 */
 
OAA_AE_SIDEBAR.ElementView.showTree = function (flag) {

  var etr = OAA_AE_SIDEBAR.ElementView.element_type_results;

  if (etr && etr.is_tree) {
  
    OAA_AE_SIDEBAR.ElementView.showTreeControls(true);

    var tree_view = new OpenAjax.a11y.formatters.TreeViewOfFilteredCacheItemResults(etr);
    this.tree_view = tree_view;

    tree_view.setOpenState(flag);

    var visible_tree_items  = tree_view.getOpenCacheItemResults();

    OAA_AE_SIDEBAR.ElementView.visible_tree_items = visible_tree_items;
 
    OAA_AE_SIDEBAR.ElementView.setView();    
    
  }
  
  


};

/**
 * @function OAA_AE_SIDEBAR.ElementView.hideTreeControls
 *
 * @desc hides tree controls
 */
 
OAA_AE_SIDEBAR.ElementView.hideTreeControls = function () {

  var tree_controls = document.getElementById('ID_HBOX_ELEMENT_VIEW_EXPAND');
  tree_controls.setAttribute('hidden', 'true');

  var show_tree = document.getElementById('ID_BUTTON_SHOW_TREE');
  show_tree.setAttribute('disabled', 'true');
  
  var order_treecol = document.getElementById('ID_TREECOL_ELEMENT_VIEW_ORDER');
  order_treecol.setAttribute('hidden', 'false');
  
  var primary_treecol = document.getElementById('ID_TREECOL_ELEMENT_VIEW_ELEMENT');
  primary_treecol.setAttribute('primary', 'false');
   
};

/**
 * @function OAA_AE_SIDEBAR.ElementView.clear
 *
 * @desc Clears the cache item view with a "updating" message
 */
 
OAA_AE_SIDEBAR.ElementView.clear = function () {

  this.visible_tree_items = [];      
  
  this.sidebar_title = "no title";
  
  this.element_type_results = null;
  
  this.filtered_cache_item_results = [];
  this.cache_item = null;

  var row = { level : 0,
              container : false,
              open : false,
            
              message : OAA_AE_SIDEBAR.ElementView.MSG_UPDATING,
              message_prop : 'message'
            };
  
  this.visible_tree_items.push(row);               
  
  this.setView();
  
  this.cacheItemButtonClear();
  
  OAA_AE_SIDEBAR.ElementResultView.clear();

};


/**
 * @function OAA_AE_SIDEBAR.ElementView.updateViewNameAndSummary
 *
 * @desc Initializes and shows the rule category and summary views
 */
  
OAA_AE_SIDEBAR.ElementView.updateViewNameAndSummary = function () {

  var etr = this.element_type_results;

  if (etr) {
//    OAA_AE_SIDEBAR.logger.debug("  ETR: " + etr + " getResultSummary: " + etr.getNodeResultSummary().percent_passed);
    OAA_AE_SIDEBAR.setSummaryValues(etr.getNodeResultSummary());
  } 
  else {  
    OAA_AE_SIDEBAR.setSummaryValues();
  }

  var view_name  = window.document.getElementById('ID_VIEW_NAME');

  if (!view_name) return;

  view_name.setAttribute('value', OAA_AE_SIDEBAR.view.nls_title);
  view_name.setAttribute('control', 'ID_TREE_ELEMENT_VIEW_ITEMS');

};


/**
 * @function OAA_AE_SIDEBAR.ElementView.update
 *
 * @desc Shows a tree of cache items
 */
 
OAA_AE_SIDEBAR.ElementView.update = function () {

  var tree = window.document.getElementById('ID_TREE_ELEMENT_VIEW_ITEMS');

  if (!tree) return;

  this.initStrings();

  var prefs = OAA_WEB_ACCESSIBILITY_PREF.util.getPreferences();
  
  this.element_type_results = null;
  
  this.filtered_cache_item_results = [];
  this.cache_item = null;

  var sidebar_option = OAA_AE_SIDEBAR.view.option;
  
//  OAA_AE_SIDEBAR.logger.debug("Sidebar Name: " + OAA_AE_SIDEBAR.view.nls_title + "  Option: " + sidebar_option);

  var etr = OAA_AE_SIDEBAR.evaluation_result.getCacheItemsByElementType(sidebar_option, prefs.show_results_filter_value); 

//  OAA_AE_SIDEBAR.logger.debug("  ETR: " + etr + " getResultSummary: " + etr.getNodeResultSummary().percent_passed);

  this.element_type_results = etr;
  this.tree_view = null;
  
  if (etr.is_tree) this.showTreeControls(true);
  else this.hideTreeControls();

  OAA_AE_SIDEBAR.ElementView.setRuleButtons();

  if (etr.cache_item_results.length) {
    var tree_view = new OpenAjax.a11y.formatters.TreeViewOfFilteredCacheItemResults(etr);
    this.tree_view = tree_view;
  
    this.visible_tree_items  = tree_view.getOpenCacheItemResults();
    
    
  }
  else {

    this.visible_tree_items = [];          

    var row = { level : 0,
                container : false,
                open : false,
            
                message : OAA_AE_SIDEBAR.ElementView.MSG_NO_ELEMENTS,
                message_prop : 'message'
              };
  
    this.visible_tree_items.push(row);  
    

  }
   
//  OAA_AE_SIDEBAR.logger.debug("IS A TREE: " + this.element_type_results.is_tree + " Item Count:" + this.visible_tree_items.length);

  // Update the columns displayed
    
  OAA_AE_SIDEBAR.ElementView.updateViewNameAndSummary();  

  this.setView();

  OAA_AE_SIDEBAR.showCurrentRulesetInformation();

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;
  OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_VIEW);

};

/**
 * @function OAA_AE_SIDEBAR.ElementView.setView
 *
 * @desc Updates tree view
 */
 
OAA_AE_SIDEBAR.ElementView.setView = function () {  

//  OAA_AE_SIDEBAR.logger.debug("Update View: ITEMS " + OAA_AE_SIDEBAR.ElementView.visible_tree_items.length);

  var tree  = window.document.getElementById('ID_TREE_ELEMENT_VIEW_ITEMS');
  
  var visible_tree_items = OAA_AE_SIDEBAR.ElementView.visible_tree_items;

  var treeView = {
     visibleData : visible_tree_items,
     
    selection   : null,
    treeBox     : null,
 
    isSeparator: function(row){ return false; },
    isSorted: function(){ return false; },
    getImageSrc: function(row,col){ return null; },        
    getColumnProperties: function(colid,col){ return ""},
    cycleHeader : function(col, elem) {},

    setTree     : function(treeBox) { 
      this.treeBox = treeBox; 
    },
        
    get rowCount() { 
      return this.visibleData.length;
    },
     
     getCellText : function(row,column) {
     
       var item      = this.visibleData[row];
       
//       OAA_AE_SIDEBAR.logger.debug("  Cell Text: " + row + " of " + this.rowCount + ": " + column.id);

       switch (column.id) {
       
       case "ID_TREECOL_ELEMENT_VIEW_ORDER":
          return item.position;
          break;
       
       case "ID_TREECOL_ELEMENT_VIEW_ELEMENT":       
         if (item.element) return item.element;         
         if (item.message) return item.message;         
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_PASSED":
         return item.passed_label;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_WARNINGS":
         return item.warnings_label;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_VIOLATIONS":
         return item.violations_label;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_FAILURES":
         return item.failures_label;
         break;
         
       case "ID_TREECOL_ELEMENT_VIEW_MANUAL_CHECKS":
         return item.manual_checks_label;
         break;

       case "ID_TREECOL_ELEMENT_VIEW_HIDDEN":
         return item.hidden_label;
         break;
       

       default:
         break;
       }  

       return "";
     },
     isContainer: function(row) { 

       var item = this.visibleData[row];

//       OAA_AE_SIDEBAR.logger.debug("  CONTAINER: " + item.element + " " + item.container);

       return item.container; 
       
     },
     isContainerEmpty: function(row) { 

       var item  = this.visibleData[row];

       var is_container_empty = (typeof item.children !== 'object') || (item.children.length === 0);

//       OAA_AE_SIDEBAR.logger.debug("  IS EMPTY: " + item.children + " (length=" + item.children.length + ") " + is_container_empty);

       return is_container_empty; 
       
     },
     getParentIndex: function(row) {
        
       if (!this.isContainer(row)) {
         for (var t = row - 1; t >= 0 ; t--) {
           if (this.isContainer(t)) return t;
         }
       }
           
       return -1;
     },
     hasNextSibling: function(row, afterIndex) {
       var thisLevel = this.getLevel(row);
       for (var t = afterIndex + 1; t < this.visibleData.length; t++) {
         var nextLevel = this.getLevel(t);
         if (nextLevel == thisLevel) return true;
         if (nextLevel < thisLevel) break;
       }
       return false;     
     },
     isContainerOpen: function(row) { 

       var item     = this.visibleData[row];

//       OAA_AE_SIDEBAR.logger.debug("  OPEN: " + item.element + " " + item.open);

       return item.open; 
       
     },
     getLevel: function(row) { 

       var item     = this.visibleData[row];

//       OAA_AE_SIDEBAR.logger.debug("  LEVEL: " + item.element + " " + item.level);
       
       return item.level; 
       
     },
     toggleOpenState: function(row){ 

       var item = this.visibleData[row];

       if (!item.container) return;

       if (item.open) {
         item.open = false;
 
         var thisLevel = this.getLevel(row);
         var deletecount = 0;
         for (var t = row + 1; t < this.visibleData.length; t++) {
           if (this.getLevel(t) > thisLevel) deletecount++;
           else break;
         }

         if (deletecount) {
           this.visibleData.splice(row + 1, deletecount);
           this.treeBox.rowCountChanged(row + 1, -deletecount);
         }
       }
       else {
         item.open = true;
      
         var children_len = item.children.length;

         for (var i = 0; i < children_len; i++) {
           this.visibleData.splice(row + i + 1, 0, item.children[i]);
         }
      
         this.treeBox.rowCountChanged(row + 1, children_len);
       }
         
      this.treeBox.invalidateRow(row);
  
    },
    getRowProperties: function(row){ return ""},
    getCellProperties: function(row,column){

       var item     = this.visibleData[row];
       
       var property_value = "";

       switch (column.id) {

       case "ID_TREECOL_ELEMENT_VIEW_ORDER":
          property_value = item.position_prop;
          break;
       
       case "ID_TREECOL_ELEMENT_VIEW_ELEMENT":       
         if (item.message_prop) property_value = item.message_prop;         
         if (item.element_prop) property_value = item.element_prop;         
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_PASSED":
         property_value = item.passed_prop;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_WARNINGS":
         property_value = item.warnings_prop;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_VIOLATIONS":
         property_value = item.violations_prop;
         break;
       
       case "ID_TREECOL_ELEMENT_VIEW_FAILURES":
         property_value = item.failures_prop;
         break;
         
       case "ID_TREECOL_ELEMENT_VIEW_MANUAL_CHECKS":
         property_value = item.manual_checks_prop;
         break;

       case "ID_TREECOL_ELEMENT_VIEW_HIDDEN":
         property_value = item.hidden_prop;
         break;
              
       default:
         break;
       }  

       return property_value;
     
     }
  };
   
  tree.view = treeView;

//  OAA_AE_SIDEBAR.logger.debug("Update View: TREE " + tree.id + " " + tree.view + " " + treeView);

  this.cache_item_result = null;    
  
  this.cacheItemButtonClear();
  OAA_AE_SIDEBAR.ElementResultView.clear();
  
};

/**
 * @function OAA_AE_SIDEBAR.ElementView.sortRuleList
 *
 * @desc Sorts the list of rules by a sort property
 */
 
OAA_AE_SIDEBAR.ElementView.sortCacheItemList = function (column) {
  
  var tree = window.document.getElementById('ID_TREE_ELEMENT_VIEW_ITEMS');

  var column_name;
  var order = tree.getAttribute("sortDirection") == "ascending" ? 1 : -1;
  
  //if the column is passed and it's already sorted by that column, reverse sort
  if (column) {
    column_name = column.id;

    if (tree.getAttribute("sortResource") == column_name) {
      order *= -1;
    }
  } else {
    column_name = tree.getAttribute("sortResource");
  }

  //setting these will make the sort option persist
  tree.setAttribute("sortDirection", order == 1 ? "ascending" : "descending");
  tree.setAttribute("sortResource", column_name);
  
  //set the appropriate attributes to show to indicator
  var cols = tree.getElementsByTagName("treecol");

  for (var i = 0; i < cols.length; i++) {
    cols[i].removeAttribute("sortDirection");
  }

  document.getElementById(column_name).setAttribute("sortDirection", order == 1 ? "ascending" : "descending");

  var tree_view = OAA_AE_SIDEBAR.ElementView.tree_view;
  
  if (OAA_AE_SIDEBAR.ElementView.element_type_results.is_tree) OAA_AE_SIDEBAR.ElementView.showTreeControls(false);

  switch (column.id) {
       
  case "ID_TREECOL_ELEMENT_VIEW_ORDER":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('position', order);
    break;
       
  case "ID_TREECOL_ELEMENT_VIEW_ELEMENT":       
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('element', order);
    break;
       
  case "ID_TREECOL_ELEMENT_VIEW_VIOLATIONS":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('violations_count', order);
    break;
       
  case "ID_TREECOL_ELEMENT_VIEW_WARNINGS":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('warnings_count', order);
    break;
       
  case "ID_TREECOL_ELEMENT_VIEW_MANUAL_CHECKS":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('manual_checks_count', order);
    break;

  case "ID_TREECOL_ELEMENT_VIEW_PASSED":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('passed_count', order);
    break;
       
  case "ID_TREECOL_ELEMENT_VIEW_HIDDEN":
    OAA_AE_SIDEBAR.ElementView.visible_tree_items = tree_view.sortListOfCacheItemResults('hidden_count', order);
    break;

       
  default:
    break;
  }  
  
  OAA_AE_SIDEBAR.ElementView.setView();

};




/**
 * @function OAA_AE_SIDEBAR.ElementView.onSelect
 *
 * @desc Updates node results when a cache item is selected
 */
 
OAA_AE_SIDEBAR.ElementView.onSelect = function () {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;
  var tree = window.document.getElementById('ID_TREE_ELEMENT_VIEW_ITEMS');
  
  if (tree) {
  
    var index = tree.currentIndex;
    
    var visible_tree_items = OAA_AE_SIDEBAR.ElementView.visible_tree_items;
         
    if (visible_tree_items.length === 0) {
      OAA_AE_SIDEBAR.ElementView.cacheItemButtonClear();
      OAA_AE_SIDEBAR.ElementResultView.clear();  
      OAA_AE_SIDEBAR.ElementView.cache_item_result = null;
      OAA_AE_SIDEBAR.ElementView.element_number = -1;
      OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_VIEW);
    }
    else {

      var visible_tree_item = visible_tree_items[index];

      var cache_item_result  = visible_tree_item.cache_item_result;      
      OAA_AE_SIDEBAR.ElementView.cache_item_result = cache_item_result;

      var cache_item = cache_item_result.getCacheItem();
      OAA_AE_SIDEBAR.logger.debug("Index: " + index + " Position: " + cache_item_result.getOrdinalPosition() + " Cache Item: " + cache_item);  

      OAA_AE_SIDEBAR.ElementView.cacheItemButtonUpdate(cache_item_result);  

      OAA_AE_SIDEBAR.showElementInformation(cache_item_result);

      OAA_AE_SIDEBAR.highlightCacheItems(cache_item_result.getCacheItem());

      OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_SELECTION);

//      OAA_AE_SIDEBAR.ElementResultView.update(element_number, cache_item_result);  
    
    }
    
    OAA_AE_SIDEBAR.ElementView.setRuleButtons();
  } 
};


/**
 * @function OAA_AE_SIDEBAR.ElementView.setRuleButtons
 *
 * @desc Enable or disable the show and highlight element buttons 
 */
 
OAA_AE_SIDEBAR.ElementView.setRuleButtons = function() {

  var tree = window.document.getElementById('ID_TREE_ELEMENT_VIEW_ITEMS');

  var flag = true;
  if ((tree.currentIndex >= 0) && OAA_AE_SIDEBAR.ElementView.cache_item) flag = false;

//  OAA_AE_SIDEBAR.logger.debug("Tree: " + tree + "  index: " + tree.currentIndex + " item: " + OAA_AE_SIDEBAR.ElementView.cache_item + "  flag: " + flag);

  var show_rule_results         = window.document.getElementById('ID_BUTTON_SHOW_RULE_RESULTS');
  var show_element_details      = window.document.getElementById('ID_BUTTON_SHOW_ELEMENT_DETAILS');
  var show_element_in_inspector = window.document.getElementById('ID_BUTTON_SHOW_ELEMENT_IN_INSPECTOR');

  if (show_rule_results)                 show_rule_results.disabled = flag;
  if (show_element_details)           show_element_details.disabled = flag;
  if (show_element_in_inspector) show_element_in_inspector.disabled = flag;
  
};


/**
 * @function OAA_AE_SIDEBAR.ElementView.showElementResults
 *
 * @desc Show rule results associated with a element
 */
 
OAA_AE_SIDEBAR.ElementView.showElementResults = function() {

  var cir = OAA_AE_SIDEBAR.ElementView.cache_item_result;
  
  if (!cir) return;

  var node_deck = window.document.getElementById('ID_DECK_VIEW');
  node_deck.selectedIndex = 3;
    
  var label_view = window.document.getElementById('ID_VIEW_NAME'); 
  
  var msg1 = OAA_AE_SIDEBAR.ElementView.MSG_ELEMENT + " " + cir.getOrdinalPosition() + ": " + cir.getCacheItem().toString();
  OAA_AE_SIDEBAR.setSummaryValues(cir.getNodeResultSummary());

  label_view.setAttribute('value', msg1);
  
  OAA_AE_SIDEBAR.ElementResultView.update(cir);
  
};


/**
 * @function OAA_AE_SIDEBAR.ElementView.showshowElementProperties
 *
 * @desc Show information about the currently selected element 
 */
 
OAA_AE_SIDEBAR.ElementView.showElementProperties = function () {

//  OAA_AE_SIDEBAR.logger.debug("Cache Item Information: " + OAA_AE_SIDEBAR.ElementView.cache_item);

  if (OAA_AE_SIDEBAR.ElementView.cache_item) {
    if (!OAA_AE_SIDEBAR.cache_item_properties_dialog || 
        !OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties) {
      OAA_AE_SIDEBAR.showCacheItemPropertiesDialog(OAA_AE_SIDEBAR.ElementView.cache_item);
    } 
  }   

};

/**
 * @function OAA_AE_SIDEBAR.ElementView.showElementInInspector
 *
 * @desc Show element in debugger
 */
 
OAA_AE_SIDEBAR.ElementView.showElementInInspector = function () {

  if (OAA_AE_SIDEBAR.ElementView.cache_item) {
    if (OAA_AE_SIDEBAR.ElementView.cache_item.dom_element) node = OAA_AE_SIDEBAR.ElementView.cache_item.dom_element.node;
    else node = OAA_AE_SIDEBAR.ElementView.cache_item.node;
  }

  var inspector = OAA_AE_SIDEBAR.evaluation_result.doc.defaultView.InspectorUI;
  
//  OAA_AE_SIDEBAR.logger.debug(" Inspector: " + inspector + " " + node );


};


/* ---------------------------------------------------------------- */
/*                    Cache Item Information Button                 */ 
/* ---------------------------------------------------------------- */

/**
 * @function OAA_AE_SIDEBAR.ElementView.cacheItemButtonClear
 *
 * @desc Clears the rules in the rules tabs
 */
 
OAA_AE_SIDEBAR.ElementView.cacheItemButtonClear = function () {

  var button_cache_item_info   = window.document.getElementById('ID_BUTTON_ELEMENT_VIEW_RULE_RESULTS');

  if (button_cache_item_info) {
    var label   = OAA_AE_SIDEBAR.ElementView.MSG_ELEMENT + " " + OAA_AE_SIDEBAR.ElementView.MSG_ELEMENT;
    
    button_cache_item_info.setAttribute("label", label);
    button_cache_item_info.setAttribute('tooltiptext', OAA_AE_SIDEBAR.ElementView.MSG_NO_ELEMENT_SELECTED);
    button_cache_item_info.setAttribute("disabled",    "true");
  }
  
  if (OAA_AE_SIDEBAR.cache_item_properties_dialog && 
      OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties) {
    OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties.clear();
  }

  this.cache_item = null;
  
};

/**
 * @function OAA_AE_SIDEBAR.ElementView.cacheItemButtonUpdate
 *
 * @desc Updates the lable of the show cache item information button
 *
 * @param {CacheItemResult}  cache_item_result  - cache item result object    
 */
 
OAA_AE_SIDEBAR.ElementView.cacheItemButtonUpdate = function (cache_item_result) {

  var button_cache_item_info   = window.document.getElementById('ID_BUTTON_SHOW_RULE_RESULTS');

  if (!button_cache_item_info) return;

  if (cache_item_result) {
  
    var cache_item = cache_item_result.getCacheItem();
    var position   = cache_item_result.getOrdinalPosition();
  
    var label   = OAA_AE_SIDEBAR.ElementView.MSG_ELEMENT + " " + position + " " + OAA_AE_SIDEBAR.ElementView.MSG_INFORMATION;
    var tooltip = OAA_AE_SIDEBAR.ElementView.MSG_MORE_INFORMATION_RULE + ": " + cache_item.toString();
    
    button_cache_item_info.setAttribute("label", label);
    button_cache_item_info.setAttribute("tooltiptext",  tooltip);
    button_cache_item_info.setAttribute("disabled", "false");

    if (OAA_AE_SIDEBAR.cache_item_properties_dialog && 
        OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties) {
      OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties.update(cache_item);
    }
    
    this.cache_item = cache_item;

  }  
  else {
    this.cacheItemButtonClear();
    
    if (OAA_AE_SIDEBAR.cache_item_properties_dialog && 
        OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties) {
      OAA_AE_SIDEBAR.cache_item_properties_dialog.cache_item_properties.clear();
    }
  }
};



